package com.banshuibao.bsb_android.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.IntentManager;

/**
 * Created by zyh on 2018/4/19.
 */

public class HomeBottomMenu {

    private MyPopupWindow popupWindow;

    private Context context;

    private View answer, record, one_to_one, offer, close;

    private String role;

    public HomeBottomMenu(Context context) {
        this.context = context;
    }

    public void setRole(String role) {
        this.role = role;
    }

    private void initHomeBottomMenu() {
        if (popupWindow == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.menu_home_bottom, null);
            popupWindow = new MyPopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setTouchable(true);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setAnimationStyle(R.style.Menu_Home_Bottom_Animation);
            answer = view.findViewById(R.id.answer);
            record = view.findViewById(R.id.record);
            one_to_one = view.findViewById(R.id.one_to_one);
            offer = view.findViewById(R.id.offer);
            close = view.findViewById(R.id.close);

            answer.setOnClickListener(l);
            record.setOnClickListener(l);
            one_to_one.setOnClickListener(l);
            offer.setOnClickListener(l);
            close.setOnClickListener(l);
        }
    }

    private View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.answer:
                    IntentManager.goIfAnswerActivity(context);
                    break;
                case R.id.record:
                    IntentManager.goRecorderContentActivity(context);
                    break;
                case R.id.one_to_one:
                    IntentManager.goConsultingOfficeActivity(context);
                    break;
                case R.id.offer:
                    IntentManager.goToAnswerListActivity(context);
                    break;
                case R.id.close:
                    break;
            }
            if (popupWindow != null && popupWindow.isShowing())
                popupWindow.dismiss();
        }
    };

    /**
     * 平台用户底部菜单
     */
    public void showCommonBottomMenu(View view) {
        if (popupWindow == null) {
            initHomeBottomMenu();
            record.setVisibility(View.GONE);
            answer.setVisibility(View.GONE);
        }
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 平台专家底部菜单
     */
    public void showExpertBottomMenu(View view) {
        if (popupWindow == null) {
            initHomeBottomMenu();
        }
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 事务所咨询师底部菜单
     */
    public void showCounselorBottomMenu(View view) {
        if (popupWindow == null) {
            initHomeBottomMenu();
        }
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 事务普通用户底部菜单
     */
    public void showOfficeUserBottomMenu(View view, boolean isOpenQuick) {
        if (popupWindow == null) {
            initHomeBottomMenu();
            record.setVisibility(View.GONE);
            answer.setVisibility(View.GONE);
            offer.setVisibility(isOpenQuick ? View.VISIBLE : View.GONE);
        }
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    static class MyPopupWindow extends PopupWindow {

        public MyPopupWindow(View contentView, int width, int height, boolean focusable) {
            super(contentView, width, height, focusable);
        }

        public interface OnDismissListener {
            void onDismiss();
        }

        private OnDismissListener dismissListener;

        public void setDismissListener(OnDismissListener dismissListener) {
            this.dismissListener = dismissListener;
        }

        @Override
        public void dismiss() {
            if (dismissListener != null)
                dismissListener.onDismiss();
            super.dismiss();
        }
    }
}
